/**
 * Created with JetBrains PhpStorm.
 * User: askar
 * Date: 15.10.13
 * Time: 17:45
 * To change this template use File | Settings | File Templates.
 */
//----------START--Button-group--------------------------------------------------------------
var $ = jQuery.noConflict();
$(document).ready(function($){
var buttons,btnGroup,labelGroup,i;
    $('label.btn').click(function(e){

        buttons = $(this).parent().find('label');
        for(i=0; i <= buttons.length;i++)
        {
            $(buttons[i]).attr({'class':'btn btn-default'}).css('color','#000000');
        }
        if($(this).attr('for') === 'MenuItem_status_1'){
            $(this).toggleClass('btn-success').css('color','#ffffff');

        }
        else if($(this).attr('for') === 'MenuItem_status_0'){
            $(this).toggleClass('btn-danger').css('color','#ffffff');

        }


 //   e.stopPropagation();
   // return false;
    });
    labelGroup = $('#status_group');
    labelGroup.find(':first-child').toggleClass('btn-success');
    btnGroup =  $('#MenuItem_status');
    btnGroup.find(':first-child').removeAttr('checked').end().find(':last-child').attr('checked','checked');
//});
    var input;
    var er = $('.errorMessage');
    if(er.length>0)
    {
        for(var i=0;i<er.length;i++)
        {
            if($(er[i]).css('display') !='none')
            {
                input = $(er[i]).prev();
                input.popover({content:er.text(),placement:'right'}).popover('show');
            }
        }
        er.remove();
    }
    //Кнопка отмена
    $('#cancel').click(function(e){
        document.location.href = "admin";
    });
});

//--------------END-Button-group--------------------------------------------------------

//---------START---form-validator.js--------------------------------------------------------
function afterValidate(form,data,hasError)
{
    var $ = jQuery.noConflict();

    var elements = $(form).find('input[type="text"]');
    for(var i=0;i<elements.length;i++)
    {
        $(elements[i]).popover('destroy');
    }
    if(hasError)
    {
        for(var prop in data)
        {
            if(prop === 'MenuItem_type')
            {
                $('#typeField').parent().popover({trigger:'manual',content:data[prop]}).blur(function(){$(this).popover('show')}).blur();
            }
            $('#'+prop).addClass('error').popover({trigger:'manual',content:data[prop],'container':$('#'+prop).parent()}).blur(function(){$(this).popover('show');}).blur();
        }
        return false;
    }
    else
    {
        for(var prop in data)
        {
            $('#'+prop).after().popover('destroy');
        }
    }
    return true;
}

function afterValidateAttribute(form, attribute, data, hasError)
{
    var  $ = jQuery.noConflict();

    if(hasError)
    {

        if(attribute.id === 'MenuItem_type')
        {
            $('#typeField').parent().popover({trigger:'manual',content:data[attribute.id],'container':$('#typeField').parent()}).blur(function(){$(this).popover('show')}).blur();
            $('#'+attribute.id).focus();
            return false;
        }

        $('#'+attribute.id).addClass('error').popover({trigger:'manual',content:data[attribute.id],'container':$('#'+attribute.id).parent()}).blur(function(){$(this).popover('show')}).blur();
        $('#'+attribute.id).focus();
        return false;
    }
    else if(attribute.value.length >= 3)
    {
        $('#'+attribute.id).popover('destroy');
        $('#'+attribute.id).focus();
    }
    else if(attribute.value.length < 3)
    {
        $('#'+attribute.id).popover('show');
        $('#'+attribute.id).focus();
    }
    //
    return true;
}
//----------END-form-validator.js-------------------------------------------------

//-----------START-selectTb---------------------------------------------------------------------
function selectPack(object){
    var i,sidLen,attrLen, s,sidPack = [],attrPack = [];
    sidLen = object.sid.length;
    attrLen = object.attr.length;
    if(sidLen > 1 && attrLen > 1 && sidLen == attrLen)
    {
       for(s in object)
       {
            if(s == 'sid'){
                sidPack = object[s];
            }
            if(s == 'attr')
            {
                attrPack = object[s];
            }
       }
        for(i=0;i <= sidPack.length;i++)
        {
            selectTb(sidPack[i],attrPack[i]);
        }
    }
    else if(sidLen != attrLen)
    {
        throw new SyntaxError('Error!!! SID and ATTR prpperty length different!!!');
    }
    else
    {
        sid = object.sid;
        attribute = object.attr;
        selectTb(sid,attribute);
    }
}
function selectTb(sid,attribute)
{
var $ = jQuery.noConflict();
    var select,li,txt,id,i,selectOption,optLen;
    var select = $('#'+sid);
    var li = $(select).find("ul li");
    li.click(function(e){
        txt = $(this).text();
        id = $(this).find("a").data("id");
        $("#"+sid+" input:first-child").val(txt).attr("id",id);
        selectOption = $("#MenuItem_"+attribute+" option");
        optLen = selectOption.length;
        for(i=0;i < optLen;i+=1)
        {
            if(selectOption[i].value == id)
            {
                $(selectOption[i]).attr("selected","selected");
            }
            else
            {
                if(selectOption[i]+":selected"){
                    $(selectOption[i]).removeAttr("selected");
                }
            }
        }
    });
}

//---------END-selectTb-----------------------------------------------------------------------

